package com.mjlf.mjatis.mapper;

import com.mjlf.mjatis.config.Configuration;
import lombok.Data;
import lombok.extern.java.Log;

import java.lang.reflect.Proxy;

/**
 * @ClassName MapperProxy
 * @Author mjlft
 * @Date 2020/6/9 22:41
 * @Version 1.0
 * @Description Mapper生成代理类
 */
@Log
@Data
public class MapperProxyFactory {

    private Configuration configuration;

    public MapperProxyFactory() {
    }

    public MapperProxyFactory(Configuration configuration) {
        this.configuration = configuration;
    }


    public <T> T newMapper(Class<T> clazz){
        if(configuration.getCacheMappers().containsKey(clazz)){
            //缓存拿取
            return (T)configuration.getCacheMappers().get(clazz);
        }else {
            MapperProxy mapperProxy = new MapperProxy(this.configuration);
            mapperProxy.parseMapperMethod(clazz);
            return (T)Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, mapperProxy);
        }
    }
}
